
angular.module('LogisticsDelivery')

.controller('LogisticsDelivery.HomeCtrl', ['$scope', '$stateParams', '$filter', 'LogisticsDeliveryService', 'imanDialog', 'Config', 
    function ($scope, $stateParams, $filter, LogisticsDeliveryService, imanDialog, Config) {
        $scope.data = {
            wareHouse: ''
        };

        $scope.data.gridOptions = {
            enableRefresh: true,
            enableCheck: true,
            columnDefs: [ 
                { col: 'Container', name: '货架号' },
                { col: 'StatusName', name: '状态' },
                { col: 'Qty', name: '配送数量' },
                { col: 'ReadyTime', name: '备货时间', cls: 'delivery-column-time' },
                { col: 'StockPerson', name: '备货人' },
                { col: 'DemandDepartment', name: '要货部门' }
            ],
            data: []
        };

        var _wareHouses = [],
            _wareHouse, _queryStr, _deliveryRecords;

        $scope.onSelectWareHouse = function () {
            selectWareHouse();
        };
        $scope.search = function (queryStr) {
            if (queryStr) {
                $scope.data.gridOptions.data = $filter('filter')(_deliveryRecords, function (record) {
                    var reg = new RegExp(queryStr);
                    return reg.test(record.Container) || reg.test(record.StockPerson);
                });
            } else {
                $scope.data.gridOptions.data = _deliveryRecords;
            }
        };
        $scope.doRefresh = function (operate, item) {
            LogisticsDeliveryService.getDeliveryRecords(_wareHouse, -1, true).then(function (data) {
                _deliveryRecords = data;
                $scope.data.gridOptions.data = data;
            }).finally(function () {
                $scope.$broadcast('scroll.refreshComplete');
            });
        };
        $scope.delivery = function () {
            var deliveryIds = [];
            angular.forEach($scope.data.gridOptions.data, function (record) {
                if (record.Status === 0 && record.checked) {
                    deliveryIds.push(record._id);
                }
            });
            if (deliveryIds.length === 0) {
                imanDialog.toastBottom('请选择待配送记录');
                return;
            }
            imanDialog.showLoading();
            LogisticsDeliveryService.updateDeliveryStatus(deliveryIds).then(function () {
                return LogisticsDeliveryService.getDeliveryRecords(_wareHouse, -1, true);
            }).then(function (data) {
                _deliveryRecords = data;
                $scope.data.gridOptions.data = data;
            }).finally(function () {
                imanDialog.hideLoading();
            });
        };

        $scope.$on('$ionicView.afterEnter', init);

        function init () {
            imanDialog.showLoading();
            var wareHouse = $stateParams.wareHouseId;
            if (!wareHouse) {
                var temp = Config.get('LogisticsDeliveryWareHouse', {});
                $scope.data.wareHouse = temp.Name || '';
                _wareHouse = temp._id || '';
            } else {
                $scope.data.wareHouse = $stateParams.wareHouseName;
                _wareHouse = wareHouse;
                Config.set('LogisticsDeliveryWareHouse', {
                    _id: wareHouse,
                    Name: $scope.data.wareHouse 
                });
            }
            LogisticsDeliveryService.getWareHouses().then(function (wareHouses) {
                _wareHouses = wareHouses;
                if (!_wareHouse) {
                    selectWareHouse();
                } else {
                    LogisticsDeliveryService.getDeliveryRecords(_wareHouse, -1, true).then(function (data) {
                        _deliveryRecords = data;
                        $scope.data.gridOptions.data = data;
                    });
                }
            }).finally(function () {
                imanDialog.hideLoading();
            });
        }

        function selectWareHouse () {
            imanDialog.showList({
                title: '请选择仓库',
                list: _wareHouses,
                valueField: '_id',
                displayField: 'Name',
                value: _wareHouse
            }).then(function (select) {
                $scope.data.wareHouse = select.Name;
                _wareHouse = select._id;
                Config.set('LogisticsDeliveryWareHouse', select);
                LogisticsDeliveryService.getDeliveryRecords(_wareHouse, -1, true).then(function (data) {
                    _deliveryRecords = data;
                    $scope.data.gridOptions.data = data;
                });
            });
        }
    }
]);